package top.mypath.service;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import top.mypath.model.OrderDetail;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author LiYalin
 */
@Slf4j
public class OrderDetailExcelListener implements ReadListener<OrderDetail> {

    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    /**
     * 缓存的数据
     */
    private List<OrderDetail> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    public List<OrderDetail> getCachedDataList() {
        return cachedDataList;
    }

    public void setCachedDataList(List<OrderDetail> cachedDataList) {
        this.cachedDataList = cachedDataList;
    }

    @Override
    public void invoke(OrderDetail orderDetail, AnalysisContext analysisContext) {
        if(StringUtils.isBlank(orderDetail.getInvoiceCode())&& new BigDecimal(0).compareTo(orderDetail.getSubtotal())<0) {
            cachedDataList.add(orderDetail);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
